/*
** drawing.c for zomzom in /home/goupil_r//GameInLab/zomzom/src
**
** Made by robin goupil
** Login   <goupil_r@epitech.net>
**
** Started on  Fri Nov 16 16:18:51 2012 robin goupil
** Last update Sun Nov 18 16:21:45 2012 robin goupil
*/

#include <stdlib.h>
#include "include/struct.h"
#include "include/player.h"
#include "include/zombie.h"
#include "include/world.h"
#include "include/drawing.h"

void	apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
  SDL_Rect offset;

  offset.x = x;
  offset.y = y;
  SDL_BlitSurface(source, NULL, destination, &offset);
}

void		draw_background(t_world *world)
{
  apply_surface(0, 0, world->background, world->screen);
}

void		draw_zombies(t_world *world)
{
  t_sprite	*sprite;
  t_zombie	*zombie;
  SDL_Rect	pos;

  zombie = world->zombie;
  while (zombie != NULL)
    {
      sprite = zombie->biped->sprite;
      if (sprite != NULL)
	{
	  pos.x = zombie->biped->pos.x;
	  pos.y = zombie->biped->pos.y;
	  SDL_BlitSurface(sprite->image, NULL, world->screen, &pos);
	}
      zombie = zombie->next;
    }
}

void		draw_player(t_world *world)
{
  t_sprite	*sprite;
  t_player	*player;
  SDL_Rect	pos;

  sprite = world->player->biped->sprite;
  player = world->player;
  if (sprite != NULL)
    {
      pos.x = player->biped->pos.x;
      pos.y = player->biped->pos.y;
      SDL_BlitSurface(sprite->image, NULL, world->screen, &pos);
    }
}

void		draw_reticle(t_world *world)
{
  t_sprite	*sprite;
  t_reticle	*reticle;
  SDL_Rect	pos;

  sprite = world->reticle->sprite;
  reticle = world->reticle;
  if (sprite != NULL)
    {
      pos.x = reticle->pos.x;
      pos.y = reticle->pos.y;
      SDL_BlitSurface(sprite->image, NULL, world->screen, &pos);
    }
}

void		draw(t_world *world)
{
  draw_background(world);
  draw_zombies(world);
  draw_player(world);
  draw_reticle(world);
  SDL_Flip(world->screen);
}
